package com.yonyou.cyxdms.basedata.service.controller.configure;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yonyou.cyx.framework.controller.base.BaseController;
import com.yonyou.cyxdms.basedata.service.dto.CarparamDTO;
import com.yonyou.cyxdms.basedata.service.dto.ConfigureDTO;
import com.yonyou.cyxdms.basedata.service.dto.VsProductDTO;
import com.yonyou.cyxdms.basedata.service.entity.product.VsProductPO;
import com.yonyou.cyxdms.basedata.service.service.color.ColorService;
import com.yonyou.cyxdms.basedata.service.service.product.VsProductService;
import com.yonyou.cyxdms.basedata.service.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * CarparamController
 *
 * @author zhangsx
 * @since 2018-09-06
 */
@RestController
@RequestMapping("/api/carparam")
@Api(tags = {"车型相关接口"})
@SuppressWarnings("unchecked")
public class CarparamController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(CarparamController.class);

    @Autowired
    public VsProductService vsProductService;

    @Autowired
    ColorService iCarparamcolorService;

    /**
     * 分页查询数据
     *
     * @param queryCondition 查询条件
     * @return com.baomidou.mybatisplus.core.metadata.IPage 分页对象
     * @author zhangsx
     * @since 2018-09-06
     */
    @ResponseBody
    @GetMapping("/")
    @SuppressWarnings("unchecked")
    public IPage<Map<String, Object>> getList(@RequestParam Map<String, Object> queryCondition) {
        Page<Map<String, Object>> page = new Page(1, 2);
        return vsProductService.selectPageBysql(page, queryCondition);
    }

    /**
     * 根据id查询
     *
     * @param id 数据主键ID
     * @return Map<String                                                                                                                               ,                                                                                                                               Object> 数据对应的map 结构
     * @author zhangsx
     * @since 2018-09-06
     */
    @ResponseBody
    @GetMapping(value = "/{id}")
    @SuppressWarnings("unchecked")
    public Map<String, Object> getById(@PathVariable("id") Integer id) {
        return vsProductService.getById(id);
    }

    /**
     * configure进行数据新增
     *
     * @param configureDTO 需要保存的DTO //字段不全后面加
     * @return Map<String                                                               ,                                                               Object> 返回的数据map
     * @author zhangsx
     * @since 2018-09-06
     */
    @ResponseBody
    @PostMapping(value = "/configureAndmodel")
    @ResponseStatus(HttpStatus.CREATED)
    @SuppressWarnings("unchecked")
    public Map<String, Object> insert(@RequestBody ConfigureDTO configureDTO) {
        logger.debug("------configureDTO分发完成开始存储-----------");
        logger.debug("configureDTO{}", configureDTO);
        CarparamDTO carparamDTO = new CarparamDTO();
        Map<String, Object> map = null;
        String appid = "ff7555e8ac239560";

        //数据来源的id
        carparamDTO.setSourceChannel(20001005);
        carparamDTO.setSourceId(configureDTO.getSourceId());
        carparamDTO.setSerialId(configureDTO.getModelDTO().getSeriesId());
        carparamDTO.setConfigCode(configureDTO.getConfigureCode());
        carparamDTO.setConfigName(configureDTO.getConfigureName());
        carparamDTO.setIsValid(configureDTO.getIsValid());
        carparamDTO.setIsRefit(configureDTO.getIsRefit());
        carparamDTO.setDisPlacement(configureDTO.getDisplacement());
        carparamDTO.setTransMission(configureDTO.getTransMission());
        //driveApproach是非必传字段
        carparamDTO.setGrade(configureDTO.getGrade());
        carparamDTO.setCarBody(configureDTO.getCarBody());
        //enableDate暂时不设置
        //disableDate暂时不设置
        carparamDTO.setFuelWay(configureDTO.getFuelWay());
        //changeAt
        carparamDTO.setModeId(configureDTO.getModelId());
        //modelCode
        carparamDTO.setModelCode(configureDTO.getModelDTO().getModelCode());
        //modelName
        carparamDTO.setModelName(configureDTO.getModelDTO().getModelName());
        carparamDTO.setAppId(appid);
        VsProductDTO productDTO = new VsProductDTO();
        BeanUtils.copyProperties(carparamDTO, productDTO);
        if ("add".equals(configureDTO.getBizId())) {
            map = vsProductService.insert(productDTO);
        } else if ("update".equals(configureDTO.getBizId())) {
            LambdaQueryWrapper<VsProductPO> queryWrapper = new QueryWrapper<VsProductPO>().lambda();
            queryWrapper.eq(VsProductPO::getAppId, appid);
            queryWrapper.eq(VsProductPO::getSourceId, carparamDTO.getSourceId());

            VsProductPO carParamPo = vsProductService.getOne(queryWrapper);
            vsProductService.update(carParamPo.getProductId().intValue(), productDTO);
        }

        return map;
    }

    /**
     * 进行数据修改
     *
     * @param id          需要修改数据的ID
     * @param carparamDTO 需要保存的DTO
     * @return Map<String                                                                                                                               ,                                                                                                                               Object> 返回的数据map
     * @author zhangsx
     * @since 2018-09-06
     */
    @ResponseBody
    @PutMapping(value = "/{id}")
    @ResponseStatus(HttpStatus.CREATED)
    @SuppressWarnings("unchecked")
    public Map<String, Object> update(@PathVariable("id") Integer id, @RequestBody CarparamDTO carparamDTO) {
        VsProductDTO productDTO = new VsProductDTO();
        BeanUtils.copyProperties(carparamDTO, productDTO);
        return vsProductService.update(id, productDTO);
    }

    /**
     * 根据车系检索汽车车型
     *
     * @param serialId
     * @return
     */
    @GetMapping(value = "/models", produces = {"application/json;charset=UTF-8"})
    @ApiOperation(value = "根据车系检索汽车车型")
    public List<CarparamVO> queryCarSerial(@RequestParam Long serialId) {
        logger.debug("serialId:{}", serialId);

        return vsProductService.querySerial(serialId);

    }

    /**
     * 查询汽车颜色
     *
     * @param carId
     * @return
     */
    @GetMapping(value = "/colors", produces = {"application/json;charset=UTF-8"})
    @ApiOperation(notes = "汽车颜色", value = "汽车颜色")
    public List<CarparamColorVO> queryCarColor(@RequestParam Long carId) {
        logger.debug("carId:{}", carId);

        return iCarparamcolorService.queryCarColor(carId);
    }

    @GetMapping(value = "/prices", produces = {"application/json;charset=UTF-8"})
    @ApiOperation(notes = "汽车价格", value = "汽车指导价格")
    public Double queryCarColor(@RequestParam Long carId, @RequestParam Integer colorId) {
        logger.debug("carId:{},colorId:{}", carId, colorId);

        return iCarparamcolorService.queryPrice(carId, colorId);
    }


    @GetMapping(value = "/information", produces = {"application/json;charset=UTF-8"})
    @ApiOperation(notes = "汽车详细信息", value = "汽车详细信息")
    public CarBaseInfoVO queryCarInfo(@RequestParam("carId") Integer carId, @RequestParam("colorId") Integer colorId) {

        logger.debug("carId:{},colorId:{}", carId, colorId);
        return vsProductService.findCarInfo(carId, colorId);

    }

    @GetMapping(value = "/serial", produces = {"application/json;charset=UTF-8"})
    @ApiOperation(notes = "车系信息", value = "车系信息")
    public SerialVo querySerialInfo(@RequestParam("brandId") Integer brandId) {

        logger.info("brandId:{}", brandId);
        return vsProductService.querySerialInfo(brandId);

    }


    /**
     * 查询list
     *
     * @param queryCondition 查询条件
     * @return java.util.List<java.util.Map                               <                               java.lang.String                               ,                               java.lang.Object>>
     * @author guorp1
     * @since 2018/10/9
     */
    @ResponseBody
    @GetMapping("/selectListBySql")
    public List<Map<String, Object>> selectListBySql(@RequestParam Map<String, Object> queryCondition) {
        return vsProductService.selectListBySql(queryCondition);
    }


    /**
     * 查询品牌车系车型配置信息
     *
     * @param queryCondition 查询条件
     * @return java.util.Map<java.lang.String                               ,                               java.lang.Object>
     * @author renwd
     * @since 2018/11/26
     */
    @GetMapping("/queryModelByCondition")
    public Map<String, Object> queryModelByCondition(@RequestParam Map<String, Object> queryCondition) {
        return vsProductService.queryModelByCondition(queryCondition);
    }


    @GetMapping(value = "/caparamInfo", produces = {"application/json;charset=UTF-8"})
    @ApiOperation(notes = "车型分页信息", value = "车型分页信息")
    public IPage<CarparamInfoVO> queryCarparamInfo(@RequestParam("brandId") Long brandId, @RequestParam("carserialId") Long carserialId, @RequestParam("pageNum") Long pageNum, @RequestParam("pageSize") Long pageSize) {

        logger.info("brandId:{}", brandId);
        logger.info("carserialId:{}", carserialId);

        return vsProductService.queryCarparamInfo(brandId, carserialId, pageNum, pageSize);
    }

    /**
     * 根据车型ID列表查询车型名称
     *
     * @author dengqiang
     * @since 2018/12/25
     */
    @GetMapping(value = "/queryCarparamNames", produces = {"application/json;" +
            "charset=UTF-8"})
    public List<String> queryCarparamNames(@RequestParam(value = "carparamIdS", required =
            true) String carparamIdS) {

        List<String> carparamNames = new ArrayList<>();
        //遍历车型id,查询车型,给carparamNames设置车型名称
        String[] ids = carparamIdS.split(",");
        for (String id : ids) {
            VsProductPO carParamPo = vsProductService.getById(Long.parseLong(id));
            if (carParamPo != null) {
                carparamNames.add(carParamPo.getProductName());
            }
        }
        return carparamNames;
    }

    /**
     * 根据车系ID列表查询车型信息
     * 使用于  海报、线索
     *
     * @author dengqiang
     * @since 2019/1/7
     */
    @GetMapping(value = "/queryCarparamInVO", produces = {"application/json;charset=UTF-8"})
    @ApiOperation(notes = "车型信息", value = "车型信息")
    public List<GroupCarparamInVO> queryCarparamInVO(@RequestParam("carserialId") Long carserialId) {
        logger.info("carserialId:{}", carserialId);
        return vsProductService.queryCarparamInfo(carserialId);
    }


}